package com.chzc.fm.duotin.ui.mine.service;

import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.support.annotation.Nullable;

import com.chzc.fm.duotin.bean.mine.FileInfo;
import com.chzc.fm.duotin.ui.mine.utils.DownloadTask;
import com.chzc.fm.duotin.utils.FileUtils;
import com.chzc.fm.duotin.utils.LogUtil;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

import javax.net.ssl.HttpsURLConnection;

/**
 * Created by BillSinla on 2016/7/3.
 */
public class DownloadService extends Service{

    public static final String ACTION_START = "ACTION_START";
    public static final String ACTION_STOP = "ACTION_STOP";
    public static final String ACTION_UPDATE = "ACTION_UPDATE";

//    public static final int MSG_INIT = 0;


    private DownloadTask task = null;


    @Override
    public void onCreate() {
        super.onCreate();
        task = new DownloadTask(this);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if(ACTION_START.equals(intent.getAction())){
            if(task.isPause){
                task.continueDownload();

            }else {
                FileInfo fileInfo = (FileInfo) intent.getSerializableExtra("fileInfo");
                LogUtil.w(fileInfo.toString());

                task.addFileInfo(fileInfo);
            }

        }else if(ACTION_STOP.equals(intent.getAction())){
            FileInfo fileInfo = (FileInfo) intent.getSerializableExtra("fileInfo");
            LogUtil.w(fileInfo.toString());
            if(task != null){
                task.isPause = true;
            }
        }
        return super.onStartCommand(intent, flags, startId);
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

}
